package com.fingard.dsp.bank.directbank.aibank01;

import java.net.URLEncoder;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.util.TransIDHelper;

public class AIBANK01Base extends DirectBase implements ITcpClient{
	
	protected static TransIDHelper idHelper = new TransIDHelper(1,9999);
	
	protected String getBatchID(){
		return Format.DateTime12Year2Format.format(new Date())+idHelper.getNextIDStr();
	}

    protected static final String INSERTSEQ = "INSERT INTO REQSEQIDMAPPING(URID,BANKCODE,TRANSDATE,REQSEQID,BANKREQSEQID) values (?,?,?,?,?);";
    protected static final String SELECTSEQ = "SELECT BANKREQSEQID FROM REQSEQIDMAPPING WHERE REQSEQID=? AND BANKCODE=?;";
	protected String[] sendToBank(String p_reqStr) throws Exception {
        String[] retStr = new String[]{"", ""};
        if (!bankFront.serverURL.equals("") && bankFront.serverURL != null) {
            WriteBankLogLn("2.提交银行的地址：" + bankFront.serverURL);
            String charsetName = "utf-8";
            //String reqHead = "0" + p_reqStr.getBytes(charsetName).length;
            //reqHead = StringHelper.padRight(reqHead, 7, ' ');
            p_reqStr = "ReqData=" + URLEncoder.encode(p_reqStr, charsetName);//reqHead + p_reqStr;
            WriteBankLogStep2(p_reqStr);
            WebRequest tmpWebRequest = new WebRequest(bankFront.serverURL);
            tmpWebRequest.setConnectTimeout(60000);
            tmpWebRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            tmpWebRequest.setRequestProperty("connection", "Close");
            byte[] tmpToWriteBytes = p_reqStr.getBytes(charsetName);
            retStr = tmpWebRequest.upload(tmpToWriteBytes, charsetName);
            WriteBankLogStep3(retStr[1]);

        } else if (bankFront.serverIP != null && bankFront.serverPort != null) {
            WriteBankLogLn("2.提交银行的地址：" + bankFront.serverIP + ":" + bankFront.serverPort);
            TcpPortClient tmpClient = new TcpPortClient(bankFront.serverIP, Integer.parseInt(bankFront.serverPort), this);
            String tmpCharset = "GBK";
            byte[] tmpBytesBody = p_reqStr.getBytes(tmpCharset);

            String tmpHead = "0" + tmpBytesBody.length;
            for (int i = tmpHead.length(); i < 7; i++)
                tmpHead += " ";
            // WriteBankLogStep2(tmpHead+p_reqStr);
            byte[] tmpBytesHead = tmpHead.getBytes(tmpCharset);

            byte[] tmpBytesSend = new byte[tmpBytesHead.length + tmpBytesBody.length];
            System.arraycopy(tmpBytesHead, 0, tmpBytesSend, 0, tmpBytesHead.length);
            System.arraycopy(tmpBytesBody, 0, tmpBytesSend, tmpBytesHead.length, tmpBytesBody.length);

            WriteBankLogStep2(tmpHead + p_reqStr);
            TcpReceiver tmpReceiver = tmpClient.sendMessage(tmpBytesSend);
            if (StringHelper.isNullOrEmpty(tmpReceiver.connState)) {
                String tmpBkStr = new String(tmpReceiver.recBytes, 0, tmpReceiver.readedLength, tmpCharset);
                WriteBankLogStep3(tmpBkStr);
            }
            retStr[0] = tmpReceiver.connState;
            if (tmpReceiver.isCompleted) {
                retStr[1] = new String(tmpReceiver.recBytes, tmpReceiver.contentStartIndex, tmpReceiver.contentLength, tmpCharset);
            } else {
                retStr[1] = tmpReceiver.getConnMsg();
                WriteBankLogLn(retStr[1]);
            }
        }

        return retStr;
    }

	@Override
	public void getContentLength(TcpReceiver p_receiver) throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	public static void getBean() throws Exception {
		// TODO Auto-generated method stub
		
	}

}
